//
//  QCTabBar.swift
//  SwiftDemoWithSina
//
//  Created by qu on 2017/1/16.
//  Copyright © 2017年 Apple. All rights reserved.
//

import UIKit

class QCTabBar: UITabBar {
    //声明一个闭包
    var closure:(()->())?
    
    override init(frame: CGRect){
        
        super.init(frame:frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI(){
        
        addSubview(composeBtn)
    }
    //懒加载自定义的btn控件
    lazy var composeBtn: UIButton = {
        //创建一个btn
        let btn = UIButton()
        //设置图片
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), for: UIControlState.normal)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: UIControlState.normal)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
        //添加监听方法
        btn.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
        //自适应
        btn.sizeToFit()
        //返回
        return btn
    }()
    
   @objc private func buttonClick(){
    
      closure?()

    }
    //布局子视图
    override func layoutSubviews() {
        super.layoutSubviews()
        //确定每个btn的宽度
        let W = KSCREENW * 0.2
        var index: CGFloat = 0
        
        for view in subviews {
            
            if view.isKind(of: NSClassFromString("UITabBarButton")!) {
                view.frame.size.width = W
                view.frame.origin.x = index * W
                
                index += 1
                
                if index == 2 {
                    index += 1
                }
            }
            
        }
        //设置btn的frame
       composeBtn.center.x = frame.width * 0.5
       composeBtn.center.y = frame.height * 0.5
    }
    
}
